package user_assets_usdt

import (
	"context"
	"errors"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUsdtWalletInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户USDT钱包信息 - 查询
func NewUserUsdtWalletInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUsdtWalletInfoLogic {
	return &UserUsdtWalletInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserUsdtWalletInfoLogic) UserUsdtWalletInfo() (resp *types.UserUSDTWalletsInfoResponse, err error) {
	request := rpc.UserUsdtWalletsInfoRequest{}
	copier.Copy(&request, &types.UserUSDTWalletsInfoRequest{})
	request.UserId = headInfo.GetTokenUid(l.ctx)
	if request.UserId == 0 {
		return &types.UserUSDTWalletsInfoResponse{
			UserUSDTWalletsInfoList: []*types.UserUsdtWalletsInfo{},
		}, errors.New("user is not exist")
	}
	info, err := l.svcCtx.AssetsRpc.UserChannel.UserUsdtWalletsInfo(l.ctx, &request)
	var dataList []*types.UserUsdtWalletsInfo

	if err != nil {
		return &types.UserUSDTWalletsInfoResponse{
			UserUSDTWalletsInfoList: dataList,
		}, err
	}
	if len(info.UserUsdtWalletsInfoList) == 0 {
		return &types.UserUSDTWalletsInfoResponse{
			UserUSDTWalletsInfoList: dataList,
		}, nil
	}
	_ = copier.Copy(&dataList, info.UserUsdtWalletsInfoList)
	for i := 0; i < len(dataList); i++ {
		usdtType, err := strconv.ParseInt(info.UserUsdtWalletsInfoList[i].UsdtType, 10, 32)
		if err != nil {
			return &types.UserUSDTWalletsInfoResponse{
				UserUSDTWalletsInfoList: dataList,
			}, nil
		}
		dataList[i].UsdtType = int32(usdtType)
	}

	return &types.UserUSDTWalletsInfoResponse{
		UserUSDTWalletsInfoList: dataList,
	}, nil
}
